<template>
	<el-form-item>
		<el-button
			v-for="(item, index) in buttonsText"
			:key="index"
			@click="handleClick(index)"
			size="small"
			:disabled="loading"
		>
			{{ item }}
		</el-button>
	</el-form-item>
</template>

<script>
import {
	rangeToday,
	rangeYesterday,
	rangeLastMonth,
	rangePastNDays,
	rangeCurrentMonth,
	rangeCurrentPeriod
} from '@/utils/datetimes'

export default {
	props: {
		kind: { type: String, default: '' },
		loading: { type: Boolean, default: false },
		action: { type: Function, default: ([]) => Void }
	},
	data() {
		return {
			buttonsText: []
		}
	},
	watch: {
		kind: {
			handler(newVal, oldVal) {
				this.setButtonsText(newVal)
			},
			immediate: true,
			deep: true
		}
	},
	created() {
		this.setButtonsText(this.kind)
	},
	methods: {
		setButtonsText(k) {
			switch (k) {
				case 'shortcutForButtons':
					this.buttonsText = [
						this.$t('common.utils.current_accounting_period'),
						this.$t('common.utils.today'),
						this.$t('common.utils.yesterday'),
						this.$t('common.utils.last_7days'),
						this.$t('common.utils.last_14days'),
						this.$t('common.utils.last_30days'),
						this.$t('common.utils.last_month'),
						this.$t('common.utils.month_now')
					]
					break
				case 'shortcutsMonth':
					this.buttonsText = [
						this.$t('common.utils.month_now'),
						this.$t('common.utils.last_month'),
						this.$t('common.utils.month1_recent'),
						this.$t('common.utils.month_three')
					]
					break
				case 'shortcutForButtonsWOPeriod':
					this.buttonsText = [
						this.$t('common.utils.today'),
						this.$t('common.utils.yesterday'),
						this.$t('common.utils.last_7days'),
						this.$t('common.utils.last_14days'),
						this.$t('common.utils.last_30days'),
						this.$t('common.utils.last_month'),
						this.$t('common.utils.month_now')
					]
					break
				default:
					this.buttonsText = []
			}
		},
		handleClick(index) {
			const rangeMethods = [
				() => rangeCurrentPeriod().range,
				() => rangeToday().range,
				() => rangeYesterday().range,
				() => rangePastNDays(7).range,
				() => rangePastNDays(14).range,
				() => rangePastNDays(30).range,
				() => rangeLastMonth().range,
				() => rangeCurrentMonth().range,
				() => rangeCurrentMonth().range, // current month
				() => rangeLastMonth().range, // last month
				() => rangePastNDays(30).range, // past 30 days | 1 month
				() => rangePastNDays(90).range // past 90 days | 3 month
			]
			let methodIndex = index
			switch (this.kind) {
				case 'shortcutsMonth':
					methodIndex += 8
					break
				case 'shortcutForButtonsWOPeriod':
					methodIndex += 1
					break
			}
			this.action(rangeMethods[methodIndex].call())
		}
	}
}
</script>

<style lang="scss" scoped></style>
